package com.lanou3g.farmdoctor.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.lanou3g.farmdoctor.R;
import com.lanou3g.farmdoctor.utils.ImageUtil;
import com.lanou3g.farmdoctor.volley.VolleySingleton;

import java.util.List;

/**
 * Created by ChenFengYao on 15/11/26.
 */
public class QuestionHeadAdapter extends BaseAdapter {
    private List<String> data;
    private Context context;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;

    public QuestionHeadAdapter(List<String> data, Context context) {
        this.data = data;
        this.context = context;
        volleySingleton = VolleySingleton.getInstance();
        imageLoader = volleySingleton._getImageLoader();
    }

    @Override
    public int getCount() {
        return data==null?0:data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_single_image,
                    parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        try {
            String url = data.get(position);
            ImageLoader.ImageListener listener = imageLoader.getImageListener(viewHolder.imageView,
                    R.mipmap.ic_launcher,R.mipmap.ic_launcher);
            imageLoader.get(url,listener);

        }catch (Exception e){

            viewHolder.imageView.setVisibility(View.GONE);
        }
        return convertView;
    }

    class ViewHolder{
        ImageView imageView;

        public ViewHolder(View view) {
            imageView = (ImageView) view.findViewById(R.id.iv_item_singel);
        }
    }
}
